package gupao.concurrency.threads;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicIntegerArray;

public class AtomicIntegerArraySample {

    public static void main(String[] args) throws InterruptedException {
        AtomicIntegerArray arrayRef = new AtomicIntegerArray(10);
        int threadNum = 20;
        CountDownLatch latch = new CountDownLatch(threadNum);
        for(int i = 0; i < threadNum; i++){
            new Thread(() -> {
                for(int t = 0; t < 100; t++){
                    for(int index = 0; index < arrayRef.length(); index++){
                        arrayRef.addAndGet(index, index);
                        Utils.waitNanos(100);
                    }
                }
                latch.countDown();
            }).start();
        }
        latch.await();
        for( int i = 0; i < arrayRef.length(); i++){
            System.out.println(arrayRef.get(i));
        }
    }
}
